<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {error_back, field_type, iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {decode_config, s94_event} from "@/js/s94_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_name = this.$get('model_name', '');
    let model = S94Model.find(model_name);
    if (!model) error_back(`模型不存在：model_name=${model_name}`)
    let param_type = 'edit';

    if(in_right_admin('ModelV2FormParam/add')) btns['添加'] = function(){ iframe('model_v2_form_param/edit?'+app.param()) }
    if(in_right_admin('ModelV2FormParam/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let type_map = field_type(field_type.CAN_MODEL_FIELD, 'label');
    let handle_type_config = decode_config('无|,JSON|json,规格|sku_config,加购项|additional_config');

    let forms_config = {
      model_name: {label:'模型',name:'where[model_name][eq]',type:'hidden',value:model.name},
      param_type: {label:'参数类型',name:'where[param_type][eq]',type:'hidden',value:param_type},
      label: {label:'关键词',name:'where[label|tips][like]',type:'text_one',config:[],value:''},
      is_public: {label:'是否公开',name:'where[is_public][eq]',type:'radio',config:'全部|,是|1,否|0',value:'',button:true},
      can_edit: {label:'可编辑',name:'where[can_edit][eq]',type:'radio',config:'全部|,是|1,否|0',value:'',button:true},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: '', order:true},
      name:{name:'name', label:'字段名',width:'160px',align:'left'},
      field:{name:'field', label:'数据库字段', width:'100px'},
      label:{name:'label', label:'别名', width:'100px'},
      tips:{name:'tips', label:'提示', min_width:'100px'},
      type:{name:'type', label:'字段类型',type:'select_one',config:type_map,width:'100px'},
      can_edit:{name:'can_edit', label:'可编辑', type:'radio_switch', config:'否|0,是|1', width:'60px', can_edit:false, onchange:this.edit},
      compute_code:{name:'compute_code', label:'计算值', min_width:'100px'},
      value:{name:'value', label:'默认值', min_width:'100px'},
      validate:{name:'validate', label:'验证规则', type:'validate_rule', value:'[]', min_width:'100px'},
      handle_type: {name:'handle_type', label:'处理类型',type:'select_one',config:handle_type_config,width:'100px'},
      sort:{name:'sort', label:'排序', type:'text_one', width:'60px', can_edit:true, onchange:this.edit},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'60px', type:'action', fixed:'right'};

    return {
      model_name: model_name,
      param_type: param_type,
      config: {
        api: '/admin/model_v2_form_param/'+(is_del?'recycle':'list'),
        title: `${model.label}字段列表${is_del?'(已删除)':''}`,
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model_name;
      data.param_type = this.param_type;
      return s94Mjs.param_encode(data);
    },
    to_edit(row){
      let can_edit = in_right_admin('ModelV2FormParam/edit');
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('model_v2_form_param/edit?'+this.param(param))
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('ModelV2FormParam/edit')) return;
      ajax(`/admin/model_v2_form_param/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (res.data==0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    s94_event(['model_v2_form_param/add','model_v2_form_param/edit'], (evt)=>{
      let res = evt.data;
      if (res.data) this.$refs.list_url.list();
    })
  },
  unmounted() {
    s94_event(['model_v2_form_param/add','model_v2_form_param/edit'], false)
  }
}
</script>
